package com.software.graduationproject.modules.user.web;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.software.graduationproject.commons.redis.RedisCache;
import com.software.graduationproject.commons.util.JwtUtil;
import com.software.graduationproject.commons.util.Result;
import com.software.graduationproject.commons.util.Results;
import com.software.graduationproject.commons.util.VerifyCodeUtils;
import com.software.graduationproject.modules.user.entity.User;
import com.software.graduationproject.modules.user.object.UserUpdateQo;
import com.software.graduationproject.modules.user.object.UserVo;
import com.software.graduationproject.modules.user.service.UserService;
import com.v2coding.validate.CreateGroup;
import com.v2coding.validate.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * UserController : 
 * ---------------------------------------------------------------------------------------------------------------------
 */
@Validated
@RestController
@RequestMapping("/User/User")
@Api(value = "API - 用户表", tags = {"接口"})
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisCache redisCache;

    @PostMapping("/sendSms")
    @ApiOperation("发送短信验证码")
    public Result sendSms(@RequestParam String phone) {
        boolean result = userService.sendSms(phone);
        if (result) {
            return Results.success("验证码发送成功") ;
        } else {
            return Results.failure("验证码发送失败");
        }
    }

    @PostMapping("/createUser")
    @ApiOperation("新增")
    public Result createQo(@RequestBody @Validated(CreateGroup.class) User qo) {
        userService.createQo(qo);
        return Results.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改用户表")
    public Result updateQo(@RequestBody @Validated(UpdateGroup.class) UserUpdateQo qo) {
        userService.updateQo(qo);
        return Results.success();
    }

    @GetMapping("/select")
    @ApiOperation("查询用户表详情")
    public Result<UserVo> selectVo(Long id) {
        UserVo userVo = userService.selectVo(id);
        return Results.success(userVo);
    }

//
//    @GetMapping("/selectPage")
//    @ApiOperation("查询分页")
//    public Result<Page<User>> selectPageVo() {
//        Page<User> userVoPage = userService.selectVo();
//        return Results.success(userVoPage);
//    }

    @GetMapping("/generatorCode")
    @ApiOperation("生成验证码")
    public void generatorCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession();
            response.setContentType("application/json");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
            session.setAttribute("code", verifyCode);
            // 将验证码图片转换为Base64编码
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            VerifyCodeUtils.outputImage(200, 80, byteArrayOutputStream, verifyCode);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            // 手动添加Base64编码头部信息
            String base64Image = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageBytes);
            // 将Base64编码的图片和验证码一起包含在响应中
            response.getWriter().write("{\"base64Image\": \"" + base64Image + "\", \"verifyCode\": \"" + verifyCode + "\"}");
        } catch (IOException e) {
            throw new ServiceException("生成验证码失败");
        }
    }

    @PostMapping("/login")
    public Result login(String phone, String code) {
        UserVo userVo = userService.login(phone, code);
        if (userVo != null) {
            return Results.success(userVo);
        }
        return Results.failure("手机验证码错误");
    }

}
